package com.carter.springframework.cloud.openfeign;

import com.carter.springframework.beans.factory.config.CarterBeanDefinition;
import com.carter.springframework.beans.factory.support.BeanDefinitionBuilder;
import com.carter.springframework.beans.factory.support.BeanDefinitionRegistry;
import com.carter.springframework.context.EnvironmentAware;
import com.carter.springframework.context.ImportBeanDefinitionRegistrar;
import com.carter.springframework.context.ResourceLoaderAware;
import com.carter.springframework.core.env.Environment;
import com.carter.springframework.core.io.ResourceLoader;
import com.carter.springframework.core.type.AnnotationMetadata;

import java.util.LinkedHashSet;
import java.util.Map;

public class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, EnvironmentAware {
    /**
     * 资源加载器
     */
    private ResourceLoader resourceLoader;

    /**
     * 环境对象
     */
    private Environment environment;

    @Override
    public void setEnvironment(Environment environment) {

    }

    @Override
    public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
        registerDefaultConfiguration(metadata, registry);
        registerFeignClients(metadata, registry);
    }

    private void registerFeignClients(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
        LinkedHashSet<CarterBeanDefinition> candidateComponents = new LinkedHashSet<>();
        //获取EnableFeignClients所有属性的集合
        Map<String, Object> attrs = metadata.getAnnotationAttributes(EnableFeignClients.class.getName());
        //AnnotationTypeFilter = new AnnotationTypeFilter(FeignClient.class);
        final Class<?>[] clients = attrs == null ? null : (Class<?>[]) attrs.get("clients");
        if(clients!=null||clients.length==0){
            //生成自定义的ClassPathScanningProvider
            //ClassPathScanningCandidateComponentProvider scanner = getScanner();
        }
    }


    private void registerDefaultConfiguration(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
        //从metadata中获取关于@EnableFeignClients注解属性的键值对
        Map<String, Object> defaultAttrs = metadata.getAnnotationAttributes(EnableFeignClients.class.getName(), true);
        if (defaultAttrs!=null&&defaultAttrs.containsKey("defaultConfiguration")){
            String name;
            if (metadata.hasEnclosingClass()){
                name = "default." + metadata.getEnclosingClassName();
            }else {
                name = "default." + metadata.getClassName();
            }
            registerClientConfiguration(registry, name, defaultAttrs.get("defaultConfiguration"));
        }
    }

    private void registerClientConfiguration(BeanDefinitionRegistry registry, String name, Object defaultConfiguration) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(FeignClientSpecification.class);
        builder.addConstructorArgValue(name);
        builder.addConstructorArgValue(defaultConfiguration);

        registry.registerBeanDefinition(name + "." + FeignClientSpecification.class.getSimpleName(),builder.getBeanDefinition());
    }

}
